﻿procedure;
var
  SL: TStringList;
  idPer: string;
  MP_DSSml: TAppDataSet;
  MP_DSPrvky: TAppDataSet;
  MP_IDPrvkuPrev: String;
  MP_DatVraceniPrev, MP_DatPrirazeniAkt, MP_DatPrirazeniPrev: DateTime; 
  
  LuP_SL : TStringList;
  LuP_DS : TAppDataset;
  LuP_Prep, LuP_Filter : String;
  LuP_Index : Integer;
  
  TB_Plan_CF : TAppDataSet;
  TB_VybraneDoklady : string;
  
  MS_SL, MS_SLText: TStringList;
  MS_DS, MS_DSPresuny: TAppDataSet;
  MS_Node, MS_IDsPres: String;
  MS_i: integer;
  
  //
  procedure MS_GetKontexty(IDProcesZdroj, IDProcesCil:string; SL: TStringList);
  var
    SL1,SL2:TStringList;
    IDProcesNad : string;
    j,k: integer;
  begin
    SL.Clear;
    SL1 := TStringList.Create;
    SL2 := TStringList.Create;
    try
      StringToSL(AppServer.GetDBKeyValue(SourceName,rmPrimary,'227676,10',IDProcesZdroj,ftIDA,ftID),';',SL1);
      while j < SL1.Count do
      begin
        IDProcesNad := AppServer.GetDBKeyValue(SourceName,rmPrimary,'227676,10',SL1.Strings[j],ftID,ftIDB);
        StringToSL(AppServer.GetDBLinkKeyValue(SourceName, rmPrimary, '227676,10', IDProcesCil, IDProcesNad),';',SL2);
        k := 0;
        while k < SL2.Count do
        begin
          SL.Add(SL1.Strings[j]+';'+SL2.Strings[k]);
          k := k + 1;
        end;
        j := j + 1;
      end;
    finally
      SL1.Free;
      SL2.Free;
    end;
  end;
  
  //
  procedure MS_VytvorPresun(ZdrojNadrizeny: boolean; IDContextZdroj, IDContextCil, IDNapVVZdroj, IDNapVVCil, IDTridaZdroj, IDTridaCil: string; var IDsVytvorenych: string);
  var
    ID: string;
  begin
    MS_DSPresuny.Insert;
    if ZdrojNadrizeny then
      MS_DSPresuny.FieldByName('39894510,10').AsString := '1';
    if IDContextZdroj <> '' then
    begin
      MS_DSPresuny.FieldByName('39894480,10.ic_a').AsString := LByID(IDContextZdroj);
      MS_DSPresuny.FieldByName('39894480,10.u_a').AsString := RbyID(IDContextZdroj);
    end;
    MS_DSPresuny.FieldByName('39894481,10.ic_a').AsString := LByID(IDContextCil);
    MS_DSPresuny.FieldByName('39894481,10.u_a').AsString := RbyID(IDContextCil);
    if IDNapVVZdroj <> '' then
    begin
      MS_DSPresuny.FieldByName('39894482,10.ic_a').AsString := LByID(IDNapVVZdroj);
      MS_DSPresuny.FieldByName('39894482,10.u_a').AsString := RbyID(IDNapVVZdroj);
    end;
    if IDNapVVCil <> '' then
    begin
      MS_DSPresuny.FieldByName('39894483,10.ic_a').AsString := LByID(IDNapVVCil);
      MS_DSPresuny.FieldByName('39894483,10.u_a').AsString := RbyID(IDNapVVCil);
    end;
    if IDTridaZdroj <> '' then
    begin
      MS_DSPresuny.FieldByName('39904695,10.ic_a').AsString := LByID(IDTridaZdroj);
      MS_DSPresuny.FieldByName('39904695,10.u_a').AsString := RbyID(IDTridaZdroj);
    end;
    if IDTridaCil <> '' then
    begin
      MS_DSPresuny.FieldByName('39904694,10.ic_a').AsString := LByID(IDTridaCil);
      MS_DSPresuny.FieldByName('39904694,10.u_a').AsString := RbyID(IDTridaCil);
    end;
    MS_DSPresuny.PostNoConfirm;
    ID := MS_DSPresuny.GetPKValue;
    if IDsVytvorenych = '' then
      IDsVytvorenych := ID
    else
      IDsVytvorenych := IDsVytvorenych + ';' + ID;
  end;
  
begin
  // MiK doplnění atributů do 102,0 (spouštět jako BI)
  SL := TStringList.Create;
  try
    if not AppServer.ExistDBKeyValue(SourceName, rmPrimary, '102,0', '40344815,10') then begin
    
      // 102,0
      SL.Clear;
      SL.Values['ic'] := '40344815';        
      SL.Values['u'] := '10';                    
      SL.Values['221,1'] := 'Barva (Qi2)';  
      SL.Values['222,0'] := '12';  
      SL.Values['244,0'] := '0';    
      SL.Values['2204,0'] := '3';    
      SL.Values['2205,0'] := '1';      
      SL.Values['2257,0'] := '3';          
      SL.Values['2240,0'] := '0';            
      AppServer.InsertDBRecord(SourceName, UserLang, '102,0', False, SL);  
    
      // 103,0
      SL.Clear;
      SL.Values['ic'] := '40344816';        
      SL.Values['u'] := '10';              
      SL.Values['ic_a'] := '102';        
      SL.Values['u_a'] := '0';        
      SL.Values['ic_b'] := '40344815';        
      SL.Values['u_b'] := '10';                    
      AppServer.InsertDBRecord(SourceName, UserLang, '103,0', False, SL);            
    end;
  finally
    SL.Free;
  end;      
  // MiK konec

  // MP Kontrola vazby "Identifikační prvky pracovního vztahu"    - SPOUŠTĚT
  MP_DSPrvky := TAppdataSet.Create;
  try       
    // "Přidělené identifikační prvky"
    MP_DSPrvky.SetDSParams(SourceName, rmPrimary, '20555,10218', dsaFullNonVisible);
    MP_DSPrvky.OrderBy := '"ic_b", "u_b", "20559,10218", "20560,10218"'; //seřadit podle - ID ident prvku, Datum přidělení
    MP_DSPrvky.NoMacros := true;
    MP_DSPrvky.Open;
    while not MP_DSPrvky.EOF do      
    begin
      if MP_IDPrvkuPrev = MP_DSPrvky.GetKeyValue('', ftIDB) then //- když stejný prvek jako předchozí
      begin
        MP_DatPrirazeniAkt := MP_DSPrvky.FieldByName('20559,10218').AsDateTime;
        // - když datum přiřazení <= (dříve)  než datum ukončení vrácení předchozího (nebo není datum vrácení předch. vyplněné
        if (MP_DatPrirazeniAkt <= MP_DatVraceniPrev) or (MP_DatVraceniPrev = 0) then
        begin
          // došlo k překryvu
          if MP_DatPrirazeniAkt = MP_DatPrirazeniPrev then // přiřazeno ke stejnému datu, není možné ten předchozí ukončit dříve
          begin
            // smazat záznam 
            if MP_DatVraceniPrev > 0 then
            begin 
              // smazat předchozí
              MP_DSPrvky.Prior;
              MP_DSPrvky.Delete; // postaví se na aktuální
            end
            else // smazat aktuální
            begin
              MP_DSPrvky.Delete; // postaví se na následující, nedělá se potom Next
              MP_DSPrvky.Prior; // jít na "předchozí", aby se správně naplnili proměnné _Prev
            end;
          end
          else
          begin
            MP_DSPrvky.Prior;
            // předchozí záznam se nastaví Datum vrácení jako "Datum přiřazení" - 1 sec
            //MsgBox('IDPrvku: '+MP_IDPrvkuPrev+ '  IDPV: '+MP_DSPrvky.GetKeyValue('', ftIDA));
            MP_DSPrvky.Edit;
            MP_DSPrvky.FieldByName('20560,10218').AsDateTime := DateAdd(dpSecond, -1, MP_DatPrirazeniAkt);
            MP_DSPrvky.PostNoConfirm;
            MP_DSPrvky.Next;
          end;
        end;
      end;
      MP_IDPrvkuPrev := MP_DSPrvky.GetKeyValue('', ftIDB); 
      MP_DatVraceniPrev := MP_DSPrvky.FieldByName('20560,10218').AsDateTime;  // Datum vrácení
      MP_DatPrirazeniPrev := MP_DSPrvky.FieldByName('20559,10218').AsDateTime;
      MP_DSPrvky.Next;
    end;
  finally
    MP_DSPrvky.Free;
  end; 
  // MP end
  
  AppServer.SetSQLScript(SourceName,TargetPath + 'Reinstall_1110BI.sql');
  AppServer.SetDSTransferData(SourceName,'6269,5',nttNonLicAndNonNLP,lttAll,[mtaInsert,mtaUpdate,mtaDelete],[mtaInsert]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttNonLicAndNLP,lttNonLangOrNonNLP,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttLicAndNonNLP,lttLangOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttLicAndNLP,lttNonNLPOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttNLPOnly,lttNLPOnly,[mtaIUpdate],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttNonLicAndNonNLP,lttAll,[mtaInsert,mtaUpdate,mtaDelete],[mtaInsert]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttNonLicAndNLP,lttNonLangOrNonNLP,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttLicAndNonNLP,lttLangOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttLicAndNLP,lttNonNLPOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttNLPOnly,lttNLPOnly,[mtaIUpdate],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttNonLicAndNonNLP,lttAll,[mtaInsert,mtaUpdate,mtaDelete],[mtaInsert]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttNonLicAndNLP,lttNonLangOrNonNLP,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttLicAndNonNLP,lttLangOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttLicAndNLP,lttNonNLPOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttNLPOnly,lttNLPOnly,[mtaIUpdate],[]);
  M17059359_10;  // NEMAZAT, STANDARDNI SOUCAST REINSTALACE
  AppServer.SetSQLScript(SourceName,TargetPath + 'Reinstall_1110AD.sql');
  
  
  // ZS - legislativa 2024, nastaveni parametru
  if (legislation = '745998,10') then
  begin
    
  end;
  // ZS konec
  
  // MP - Úpravy Stavu smlouvy a Stavu dokladu - smlouvy vodárenství      - NESPOUŠTĚT
  MP_DSSml := TAppDataSet.Create;
  try
    //  "Smlouvy vodárenství - přesun hodnot údajů - reinstalační makro" (611007,10103)
    MP_DSSml.SetDSParams(SourceName, rmPrimary,'611007,10103', dsaEditNonVisible);
    MP_DSSml.Filter := '"1143560,10103"=5 ';
    MP_DSSml.NoMacros := True;
    MP_DSSml.Open;
    
    // "Stav smlouvy" - Když stav = 5 Nevyužito (nově dokončená), tak přepnout na 6 = nevyužito
    while not MP_DSSml.EOF do
    begin
      MP_DSSml.Edit;
      MP_DSSml.FieldByName('1143560,10103').AsInteger := 6;
      MP_DSSml.PostNoConfirm;
      MP_DSSml.Next;
    end;
    
    // - Nastavit "Stav smlouvy" na (novou hodnotu) Dokončená, když: "Stav smlouvy" = Potvrzená a "Stav dokladu" = Dokončen
    MP_DSSml.Filter := '"1143560,10103"=4 and "1143561,10103"=1 ';
    while not MP_DSSml.EOF do
    begin
      MP_DSSml.Edit;
      MP_DSSml.FieldByName('1143560,10103').AsInteger := 5;
      MP_DSSml.PostNoConfirm;
      MP_DSSml.Next;
    end;
    
    // - Přepnout "Stav dokladu" na Dokončen, když "Stav smlouvy" = Potvrzená a Stav dokladu je Rozpracován (nebo prázdný)       
    MP_DSSml.Filter := '"1143560,10103"=4 and ("1143561,10103"=0 or "1143561,10103" is null) ';
    while not MP_DSSml.EOF do
    begin
      MP_DSSml.Edit;
      MP_DSSml.FieldByName('1143561,10103').AsInteger := 1;
      MP_DSSml.PostNoConfirm;
      MP_DSSml.Next;
    end;
  finally
    MP_DSSml.Free;
  end;
  // MP konec
  
  //LuP: plneni udaje na vydeji (NESPOUSTET)
  LuP_SL := TStringList.Create;
  try
    AppServer.GetAKLicSL(LuP_SL);
    while LuP_Index < LuP_SL.Count do begin
      if LuP_Filter = '' then
        LuP_Filter := LuP_SL.Strings[LuP_Index]
      else
        LuP_Filter := LuP_Filter + ',' + LuP_SL.Strings[LuP_Index];
      //
      LuP_Index := LuP_Index + 1;
    end;
    if LuP_Filter <> '' then
      LuP_Filter := '"40327692,10.u" in (' + LuP_Filter + ')';
  finally
    LuP_SL.Free;
  end;
  //
  LuP_DS := TAppDataSet.Create;
  try
    LuP_DS.SetDSParams(SourceName, rmPrimary, '40327690,10', dsaReadNonVisible);
    LuP_DS.Filter := LuP_Filter;
    LuP_DS.Open;
    while not LuP_DS.EOF do begin
      LuP_Prep := LuP_DS.FieldByName('40327707,10').AsString;
      if LuP_Prep = '1' then
        LuP_Prep := '0'
      else
        LuP_Prep := '1';
      AppServer.SetDBAtrValue(SourceName, '', '29558,10', '941297,10', LuP_DS.GetKeyValue('40327692,10', ftID), LuP_Prep);
      M8092100_10(0, '', LuP_DS.GetKeyValue('', ftIDB));
      //
      LuP_DS.Next;
    end;
  finally
    LuP_DS.Free;
  end;
  //LuP end
   
  // TB - begin (NESPOUSTET)
  //Plan CF
  TB_Plan_CF := TAppDataset.Create;
  try
    TB_Plan_CF.SetDSParams(SourceName,rmDefault,'1043222,10', dsaReadNonVisible, '', '', '', '', '');
    TB_Plan_CF.Filter := '("1045614,10" = 0)';
    TB_Plan_CF.NoMacros := True;               
    TB_Plan_CF.Open;
    TB_Plan_CF.Last;
    // Vybrané doklady
    if not TB_Plan_CF.IsEmpty then
      TB_VybraneDoklady := TB_Plan_CF.FieldByName('1716589,10').AsString;
    if (TB_VybraneDoklady = '0') or (TB_VybraneDoklady = '') then
      TB_VybraneDoklady := '2047';
    TB_Plan_CF.Close;
    // Typ dokladu - cashflow = všechny
    AppServer.SetDBAtrValue(SourceName, '', '102,0', '2419,0', '1716590,10', TB_VybraneDoklady);
    // Definice časového období - Cash Flow
    AppServer.SetDBAtrValue(SourceName, '', '102,0', '2419,0', '15842079,10', '');
    // Datum konce plánu - Cash Flow - Definice
    AppServer.SetDBAtrValue(SourceName, '', '102,0', '2419,0', '15842083,10', '');
    // Počet měsíců dopředu - Cash Flow
    AppServer.SetDBAtrValue(SourceName, '', '102,0', '2419,0', '15842085,10', '');
  finally
    TB_Plan_CF.Free;
  end;
  // TB - end
  
  // MaSu - Reinstalace procesních přesunů (NESPOUSTET)
  MS_Node := GetUserGlobals('','647187,10','');
  //Zkopírováno z reinstalace na 110.0, ale tentokrát
  //pouze pro uzel 9999 (= demo) a jen pokud je cílová DT prázdná
  if (MS_Node = '9999') and (AppServer.GetDBKeyValues(SourceName,rmPrimary,'39894462,10','') = '') then
  begin
    MS_DS := TAppDataSet.Create;
    MS_SL := TStringList.Create;
    MS_SLText := TStringList.Create;
    MS_DSPresuny := TAppDataSet.Create;
    try
      //Kontextové procesní přesuny
      MS_DSPresuny.SetDSParams(SourceName,rmPrimary,'39894478,10',dsaFullNonVisible);
      MS_DSPresuny.NoMacros := true;
      MS_DSPresuny.OpenEmpty;
      MS_SLText.Add('BEGIN (Reinstall 6385476,10 --> 39894462,10)');
      //Reinstall 110.0 - Přesun vstupu/výstupu
      MS_DS.SetDSParams(SourceName,rmPrimary,'26315044,10',dsaReadNonVisible);
      MS_DS.Open;
      while not MS_DS.EOF do
      begin
        MS_IDsPres := '';
        StringToSL(AppServer.GetDBLinkKeyValue(SourceName, rmPrimary, '227676,10', MS_DS.GetKeyValue('26315045,10',ftIDA), MS_DS.GetKeyValue('26315048,10',ftIDA)),';',MS_SL);
        if MS_SL.Count > 0 then
        begin
          MS_i := 0;
          while MS_i < MS_SL.Count do
          begin
            MS_VytvorPresun(true,'',MS_SL.Strings[MS_i],MS_DS.GetKeyValue('26315048,10',ftID),MS_DS.GetKeyValue('26315045,10',ftID),'','',MS_IDsPres);
            MS_i := MS_i + 1;
          end;
        end else
        begin
          MS_GetKontexty(MS_DS.GetKeyValue('26315048,10',ftIDA),MS_DS.GetKeyValue('26315045,10',ftIDA),MS_SL);
          MS_i := 0;
          while MS_i < MS_SL.Count do
          begin
            MS_VytvorPresun(false,ElemByIndex(MS_SL.Strings[MS_i],0,';'),ElemByIndex(MS_SL.Strings[MS_i],1,';'),MS_DS.GetKeyValue('26315048,10',ftID),MS_DS.GetKeyValue('26315045,10',ftID),'','',MS_IDsPres);
            MS_i := MS_i + 1;
          end;
        end;
        if MS_IDsPres = '' then 
          MS_IDsPres := '???';
        MS_SLText.Add('  ' + MS_DS.GetPKValue + ' --> ' + MS_IDsPres);
        MS_DS.Next;
      end;
      MS_DS.Close;
      MS_SLText.Add('END (Reinstall 6385476,10 --> 39894462,10)');
      MS_SLText.Add('BEGIN (Reinstall 23273425,10 --> 39894462,10)');
      //Reinstall 110.0 - Proces pro převzetí realizátora
      MS_DS.SetDSParams(SourceName,rmPrimary,'39990170,10',dsaReadNonVisible);
      MS_DS.Open;
      while not MS_DS.EOF do
      begin
        MS_IDsPres := '';
        MS_SL.Clear;
        if (MS_DS.GetKeyValue('39990173,10',ftID) = '') and (MS_DS.GetKeyValue('39990380,10',ftIDB) = '10582733,10') then
        begin
          StringToSL(AppServer.GetDBLinkKeyValue(SourceName, rmPrimary, '227676,10', MS_DS.GetKeyValue('',ftIDA), MS_DS.GetKeyValue('',ftIDB)),';',MS_SL);
          MS_i := 0;
          while MS_i < MS_SL.Count do
          begin
            MS_VytvorPresun(true,'',MS_SL.Strings[MS_i],'','','39975202,10','39904864,10',MS_IDsPres);
            MS_i := MS_i + 1;
          end;
        end;
        if MS_SL.Count = 0 then
        begin
          MS_GetKontexty(MS_DS.GetKeyValue('',ftIDB),MS_DS.GetKeyValue('',ftIDA),MS_SL);
          MS_i := 0;
          while MS_i < MS_SL.Count do
          begin
            if MS_DS.GetKeyValue('39990173,10',ftID) <> '' then
              MS_VytvorPresun(false,ElemByIndex(MS_SL.Strings[MS_i],0,';'),ElemByIndex(MS_SL.Strings[MS_i],1,';'),'','','39904869,10','39904864,10',MS_IDsPres)
            else
              if MS_DS.GetKeyValue('39990380,10',ftIDB) = '10582733,10' then
                MS_VytvorPresun(false,ElemByIndex(MS_SL.Strings[MS_i],0,';'),ElemByIndex(MS_SL.Strings[MS_i],1,';'),'','','39975202,10','39904864,10',MS_IDsPres);
            MS_i := MS_i + 1;
          end;
        end;
        if MS_IDsPres = '' then 
          MS_IDsPres := '???';
        MS_SLText.Add('  ' + MS_DS.GetPKValue + ' --> ' + MS_IDsPres);
        MS_DS.Next;
      end;
      MS_DS.Close;
      MS_SLText.Add('END (Reinstall 23273425,10 --> 39894462,10)');
      MS_SLText.Add('BEGIN (Reinstall 30297894,10 --> 39894462,10)');
      //Reinstall 110.0 - Vykonávají ze vstupů
      MS_DS.SetDSParams(SourceName,rmPrimary,'39990382,10',dsaReadNonVisible);
      MS_DS.Open;
      while not MS_DS.EOF do
      begin
        MS_IDsPres := '';
        StringToSL(AppServer.GetDBKeyValue(SourceName,rmPrimary,'227676,10',MS_DS.GetKeyValue('39990385,10',ftID),ftIDA,ftID),';',MS_SL);
        MS_i := 0;
        while MS_i < MS_SL.Count do
        begin
          MS_VytvorPresun(false,MS_SL.Strings[MS_i],MS_SL.Strings[MS_i],MS_DS.GetKeyValue('39990384,10',ftID),'',MS_DS.GetKeyValue('',ftIDB),'39904864,10',MS_IDsPres);
          MS_i := MS_i + 1;
        end;
        if MS_IDsPres = '' then 
          MS_IDsPres := '???';
        MS_SLText.Add('  ' + MS_DS.GetPKValue + ' --> ' + MS_IDsPres);
        MS_DS.Next;
      end;
      MS_SLText.Add('END (Reinstall 30297894,10 --> 39894462,10)');
      M25557007_10('1503411', '10', 'Reinstalace procesních přesunů', SLToString(MS_SLText,#13#10));
    finally
      MS_DS.Free;
      MS_SL.Free;
      MS_SLText.Free;
      MS_DSPresuny.Free;
    end;
  end;
  // MaSu - konec
end.